package com.gxa.scdx.cloud.user.controller;

import com.gxa.scdx.cloud.user.pojo.User;
import com.gxa.scdx.cloud.user.service.ShiroService;
import com.gxa.scdx.cloud.user.service.UserService;
import io.swagger.annotations.*;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * 权限控制层
 * @author yawn
 */
@RestController
@RequestMapping(value = "/shiro")
@Api(value = "用户表(shiro)管理", tags = "用户表(shiro)管理接口API")
@ApiResponses({
        @ApiResponse(code = 400, message = "请求参数不完整或者错误"),
        @ApiResponse(code = 404, message = "找不到页面"),
        @ApiResponse(code = 405, message = "请求方式不正确，比如后台接收是post，但前端请求的是get"),
        @ApiResponse(code = 406, message = "页面地址和接口名冲突"),
        @ApiResponse(code = 415, message = "请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串"),
        @ApiResponse(code = 500, message = "后台服务逻辑错误")
})
public class ShiroController {
    @Resource
    private UserService userService;
    @Resource
    private ShiroService shiroService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiOperation(value = "登录接口", notes = "登录接口", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "password", value = "密码", paramType = "query", dataType = "String", required = true)
    })
    public Map<String, Object> Login(@RequestParam String username, @RequestParam String password, HttpServletRequest request) {
        System.out.println(request.getRequestURL());
        System.out.println(request.getLocalAddr());
        System.out.println(request.getRemoteAddr());
        System.out.println(request.getRequestURI());
        Map<String, Object> map = shiroService.selectUser(username, password);
        Object code  = map.get("code");
        if ( Integer.parseInt(String.valueOf(code))==200){
            User user  =new User();
            user.setUsername(username);
            user.setLastLoginTime(new Date());
            userService.updateByName(user);
        }
        return map;
    }

    @RequestMapping(value = "/register")
    public Map<String, Object> registerUser(@RequestParam String username, @RequestParam String password, @RequestParam String phone) {
        return userService.registerUser(username, md5(username, password), phone);
    }

    private String md5(String username, String password) {
        String hashAlgorithmName = "MD5";                   // 加密方式
        ByteSource salt = ByteSource.Util.bytes(username);  // 以账号作为盐值
        int hashIterations = 11;                            // 加密11次
        return new SimpleHash(hashAlgorithmName, password, salt, hashIterations).toString();
    }
}
